var time,score
time = 60
score = 0
var playchoose

var timeInterval
var marisaInterval_move
var kunInterval_move

var button_Array = ["recyclable_button","Foodwaste_button","Dry_button","Hazardous_button"]
var garbage_imgs_str = [
    ["newspaper_img","beerBottle_img","plasticBag_img","clothes_img","cans_img"], // 可回收垃圾
    ["leaves_img","bone_img","eggShells_img","apple_img","bananaPeel_img"], // 厨余垃圾
    ["diapers_img","ball_img","ceramics_img","plasticine_img","paperCup_img"], //干垃圾
    ["battery_img","insecticide_img","thermometer_img","potion_img","paint_img"] // 有害垃圾
]
var garbage_images = new Array()

var garbageingID


// 游戏开始
function Start() {
    console.log("开始按钮被按下")
    document.getElementById("start_button").style.display = "none"
    first()
    initiative()
    for(var i=0;i<button_Array.length;i++){
        document.getElementById(button_Array[i]).disabled = ""
    }
}

// 游戏初始化
function initiative() {

    timeInterval = setInterval(function(){
        document.getElementById("time_text").innerText = time--
        if(time==-1){
            clearInterval(timeInterval)
            over()
        }
    },1000)

    marisaInterval_move = setInterval(function(){
        document.getElementById("Marisa_Standby_img").style.display = "none"
        document.getElementById("Marisa_gif").style.display = "block"
        setTimeout(function(){
            document.getElementById("Marisa_Standby_img").style.display = "block"
        document.getElementById("Marisa_gif").style.display = "none"
        },1000)
        garbageMove1()
    },4000)

    kunInterval_move = setInterval(function(){
        if(document.getElementById(garbageingID).offsetLeft==3610){
            document.getElementById("Kun_Standby_img").style.display = "none"
            document.getElementById("Kun_gif").style.display = "block"
            setTimeout(function(){
                document.getElementById("Kun_Standby_img").style.display = "block"
                document.getElementById("Kun_gif").style.display = "none"
            },200)
        }
    },1)

}

function first(){
    document.getElementById("Marisa_Standby_img").style.display = "none"
    document.getElementById("Marisa_gif").style.display = "block"
    setTimeout(function(){
        document.getElementById("Marisa_Standby_img").style.display = "block"
    document.getElementById("Marisa_gif").style.display = "none"
    },1000)
    garbageMove1()
}

// 游戏结束
function over(){
    document.getElementById("reStart_button").style.display = "block"
    document.getElementById("reStart_button").disabled = ""
    document.getElementById("Finally_score_img").style.display = "block"
    document.getElementById("Finally_score_text").innerText = score
    document.getElementById("Finally_score_text").style.display = "block"
    for(var i=0;i<button_Array.length;i++){
        document.getElementById(button_Array[i]).disabled = "true"
    }
    clearInterval(marisaInterval_move)
    clearInterval(kunInterval_move)
    clearInterval(kunInterval_stop)
    document.getElementById("Marisa_Standby_img").style.display = "block"
    document.getElementById("Marisa_gif").style.display = "none"
    document.getElementById("Kun_Standby_img").style.display = "block"
    document.getElementById("Kun_gif").style.display = "none"
}

// 垃圾移动第一阶段函数
function garbageMove1(){
    var randomnum = Math.round(Math.random()*19)
    console.log("扔一次垃圾，垃圾开始移动")
    garbageingID = garbage_imgs_str[Math.floor(randomnum/5)][randomnum%5]

    console.log(garbageingID, document.getElementById(garbageingID).offsetLeft, document.getElementById(garbageingID).offsetTop)
    var x = 15
    var y = -15
    document.getElementById(garbageingID).style.display = "block"
    var garbageInterval_move = setInterval(function(){
        var xpx = document.getElementById(garbageingID).offsetLeft + x
        var ypx = document.getElementById(garbageingID).offsetTop + y
        document.getElementById(garbageingID).style.left = xpx + 'px'
        document.getElementById(garbageingID).style.top = ypx + 'px'
        y = y + 0.12
        console.log(x,xpx)
        if(document.getElementById(garbageingID).offsetLeft+x==4000){
            clearInterval(garbageInterval_move)
            garbageMove2()
        }
    },6)
}

// 垃圾移动第二阶段函数
function garbageMove2(type){

    var x = 15
    var y = -15
    var garbageInterval_move = setInterval(function(){
        var xpx = document.getElementById(garbageingID).offsetLeft + x
        var ypx = document.getElementById(garbageingID).offsetTop + y
        document.getElementById(garbageingID).style.left = xpx + 'px'
        document.getElementById(garbageingID).style.top = ypx + 'px'
        console.log(garbageingID, document.getElementById(garbageingID).offsetLeft, document.getElementById(garbageingID).offsetTop)
        console.log("playchoose:",playchoose,"y:",y)
        if(playchoose == 0){
            y = y + 0.14
        }else if(playchoose ==1){
            y = y + 0.11
        }else if(playchoose == 2){
            y = y + 0.091
        }else{
            y = y + 0.078
        }

        if(document.getElementById(garbageingID).offsetTop > 800){
            for(var i =0;i<4;i++){
                for(var j=0;j<5;j++){
                     document.getElementById(garbage_imgs_str[i][j]).setAttribute('style','')
                     document.getElementById(garbage_imgs_str[i][j]).style.display = "none"
                }
            }
            clearInterval(garbageInterval_move)
        }
    },5)
}

function clearStyle(){
    document.getElementById("recyclable_button_img").style.top = "-8px"
    document.getElementById("Foodwaste_button_img").style.top = "-8px"
    document.getElementById("Dry_button_img").style.top = "-8px"
    document.getElementById("Hazardous_button_img").style.top = "-8px"
}

// 可回收垃圾按钮
function recyclable_button(){
    console.log("可回收垃圾按钮被按下")
    playchoose = 0
    clearStyle()
    document.getElementById("recyclable_button_img").style.top = "-20px"
    if(garbage_imgs_str[0].indexOf(garbageingID)!=-1){
        thatRight()
    }else{
        readMore()
    }
}

// 厨余垃圾按钮
function Foodwaste_button(){
    console.log("厨余垃圾按钮被按下")
    playchoose = 1
    clearStyle()
    document.getElementById("Foodwaste_button_img").style.top = "-20px"
    if(garbage_imgs_str[1].indexOf(garbageingID)!=-1){
        thatRight()
    }else{
        readMore()
    }
}

// 干垃圾按钮
function Dry_button(){
    console.log("干垃圾按钮被按下")
    playchoose = 2
    clearStyle()
    document.getElementById("Dry_button_img").style.top = "-20px"
    if(garbage_imgs_str[2].indexOf(garbageingID)!=-1){
        thatRight()
    }else{
        readMore()
    }
}

// 有害垃圾按钮
function Hazardous_button(){
    playchoose = 3
    clearStyle()
    console.log("有害垃圾按钮被按下")
    document.getElementById("Hazardous_button_img").style.top = "-20px"
    if(garbage_imgs_str[3].indexOf(garbageingID)!=-1){
        thatRight()
    }else{
        readMore()
    }
}

// 重新开始按钮
function reStart(){
    console.log("重新开始按钮被按下")
    location.reload(true)
}

// 答对了函数
function thatRight(){
    document.getElementById("readMore_img").style.display = "none"
    document.getElementById("thatRight_img").style.display = "block"
    score = score + 100
    document.getElementById("score_text").innerText = score
}

// 答错了函数
function readMore(){
    document.getElementById("thatRight_img").style.display = "none"
    document.getElementById("readMore_img").style.display = "block"
    score = score - 100
    document.getElementById("score_text").innerText = score
}


// 加载垃圾图片
window.onload = function (){

    var img_div = document.getElementById("garbage_div")

    var img_src=["/img/可回收垃圾/报纸.png","/img/可回收垃圾/啤酒瓶.png","/img/可回收垃圾/塑料袋.png","/img/可回收垃圾/衣服.png","/img/可回收垃圾/易拉罐.png",
    "/img/厨余垃圾/菜叶.png","/img/厨余垃圾/骨头.png","/img/厨余垃圾/鸡蛋壳.png","/img/厨余垃圾/苹果.png","/img/厨余垃圾/香蕉皮.png",
    "/img/干垃圾/尿不湿.png","/img/干垃圾/皮球.png","/img/干垃圾/陶瓷.png","/img/干垃圾/橡皮泥.png","/img/干垃圾/纸杯.png",
    "/img/有害垃圾/电池.png","/img/有害垃圾/杀虫剂.png","/img/有害垃圾/温度计.png","/img/有害垃圾/药水.png","/img/有害垃圾/油漆.png"]

    for(var i=0;i<4;i++){
        for(var j=0;j<5;j++){
            garbage_images[i*5+j] = document.createElement("img")
            garbage_images[i*5+j].id = garbage_imgs_str[i][j]
            garbage_images[i*5+j].src = img_src[i*5+j]
            garbage_images[i*5+j].setAttribute("class","garbage_img")
            img_div.appendChild(garbage_images[i*5+j])
        }
    }
};